package com.weishi.project.api.controller;

import com.weishi.common.constant.Constants;
import com.weishi.framework.security.service.SysLoginService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.api.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app")
@Api(value = "ApiLogin", description = "登录相关")
public class ApiLoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginVo login)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(login.getUsername(), login.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation(value = "注销登录")
    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request)
    {
        try {
            loginService.logout(request);
            return AjaxResult.success("注销登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("注销登录失败");
        }
    }

}
